<?php

$client_obj = new client_obj();
$user_obj = new user_obj();
$task_obj = new task_obj();
$task_phorum_obj = new task_phorum_obj();

if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) {

    $actions = explode("_", $_REQUEST['action']);
    if (in_array(TASK_ACTION_WRITE, $actions)) {
        $task = $_REQUEST['task'];
        $task['added'] = date("Y-m-d H:i:s");
        $task['updated'] = '0000/00/00 00:00:00';
        $task['creator'] = $_SESSION['user']['user_id'];
        if (empty($task['created']))
            $task['created'] = date("Y-m-d");
        else
            $task['created'] = base_obj::convertDateTo($task['created']);
        if (!empty($task['task_start']))
            $task['task_start'] = base_obj::convertDateTo($task['task_start']);
        if ($task['deadline'] == '00/00/0000')
            $task['deadline'] = MAX_DEADLINE;
        $task['deadline'] = base_obj::convertDateTo($task['deadline']);

        if (!isset($_REQUEST['task']['consult']))
            $task['consult'] = 0;

        $task_phorum = array();
        $task_phorum['user_id'] = $_SESSION['user']['user_id'];
        $task_phorum['when'] = date("Y-m-d H:i:s");
        $task_phorum['type'] = 1;
        $task_phorum['text'] = base_obj::my_nl2br($task['description']);
        unset($task['description']);

        if (in_array(TASK_ACTION_COMPLETE, $actions))
            $task['status'] = COMPLETED_TASK;

        $newId = $task_obj->addTask($task);
        if (error_obj::isError($newId)) {
            error_obj::setError($newId, UNEXCEPTED);
        } else {
            $task_phorum['task_id'] = $newId;
            if (!empty($task_phorum['text'])) {
                $res = $task_phorum_obj->addPost($task_phorum);
                if (error_obj::isError($res)) {
                    error_obj::setError($res, UNEXCEPTED);
                }
            }
            if (in_array(TASK_ACTION_COMPLETE, $actions) || $task['status'] == COMPLETED_TASK) {
                $task_phorum['task_id'] = $newId;
                $task_phorum['text'] = 'Completed';
                $res = $task_phorum_obj->addPost($task_phorum);
                if (error_obj::isError($res)) {
                    error_obj::setError($res, UNEXCEPTED);
                }
            }
            if ($task['type'] == 9) {
                $operation = $_REQUEST['operation'];
                $operation['date_of_action'] = base_obj::convertDateTo($operation['date_of_action']);
                $operation['taskId'] = $newId;
                $res = $task_obj->addOperationWithUser($operation);
                if (error_obj::isError($res)) {
                    error_obj::setError($res, UNEXCEPTED);
                }
            } elseif ($task['type'] == 8) {
                $goods = $_REQUEST['goods'];
                $goods['delivery'] = base_obj::convertDateTo($goods['delivery']);
                $goods['taskId'] = $newId;
                $res = $task_obj->addOrderOfGoods($goods);
                if (error_obj::isError($res)) {
                    error_obj::setError($res, UNEXCEPTED);
                }
            }
            //$_SESSION['request']['client'] = $task['client'];

            $task['description'] = $task_phorum['text'];
            if (in_array(TASK_ACTION_SEND, $actions)) {
                $sent = $task_obj->send_notification($newId, $task, 'add', array(), true);
            } else {
                $sent = $task_obj->send_notification($newId, $task);
            }
            if (error_obj::isError($sent))
                error_obj::setError($sent, EMAIL_ERROR_NOT_SENT);
            
            $in_data = array(
                'rel_id' => $newId,
                'type' => CHANGES_TYPE_TASK,
                'status' => CHANGES_STATUS_NEW,
                'date' => time()
            );
            
            $change_obj = new change_obj();
            $change_obj->add_change($in_data);

            base_obj::redirect(ROOT . '?input=edit_task&task=' . $newId . '&result_message=new_task');
        }
    }
}


$task = array();
$task['start'] = $task['task_start'] = date("d/m/Y");
$filter = array();
$filter['active'] = 0;
if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_CLIENT) {
    $filter['clients'] = $_SESSION['user']['clients'];
}
$task['clients'] = $client_obj->getAllClients('contactPerson, klient_id, name, bad_payer', $filter, 'name ASC');
$filter = array();
$filter['active'] = 1;
$filter['client'] = 11;
$task['responsibles'] = $user_obj->getUsers('name, surname, users_id', $filter);
$task['types'] = $task_obj->getTaskTypes();
$task['statuses'] = $task_obj->getTaskStatuses();
$task['priorities'] = $task_obj->getTaskPriorities();
$task['problem_types'] = $task_obj->getTaskTypeOfProblems();
$task['operation_types'] = $task_obj->getTaskTypeOfOperations();
$task['goods_types'] = $task_obj->getGoodTypes();


$GLOBALS['template_data'] = $task;
?>
